/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
    public:
        bool isSymmetric(TreeNode* root) {
            return dfs(root->left,root->right);
        }
        bool dfs(TreeNode* p, TreeNode* q){
            if(p == nullptr && q == nullptr) return true;
            if((p == nullptr && q != nullptr) || (p!= nullptr&&q == nullptr)) return false;
    
            if(p->val != q->val)    return false;
    
            return dfs(p->left,q->right)&&dfs(p->right,q->left);
    
        }
        //dfs任务：判断左子树和右子树是否对称
        //子问题：两个节点是否相等，相等则继续检查；不相等则返回false
        //注意递归出口
    };